<?php

include_once  "function_xml_basic.php";

$kw_path = "/MINI/SECURITY/KEYWORD/FILTERS";

function GetXmlKeyWordList()
{
	global $kw_path;
	$query_string = $kw_path. "/WORD";
	$key_array = array("Name", "Type", "Enabled");
	return GetAttributList($query_string, $key_array);
}
function DelXmlSpecialKeyWord($s)
{
	global $kw_path;
	
	$query_string = $kw_path. "/WORD". "[@Name='". $s. "']";
	//print $query_string;
	DelSpecialNode($query_string);
	ApplySysKeyWordFilter();
}
function AppendXmlKeyWord($in_array)
{
	global $kw_path;
        $node_name = "WORD";
	$query_string = $kw_path;
	AppendAllAttrOfNode($query_string, $node_name, $in_array);
	ApplySysKeyWordFilter();
}

function FlushSysRuleForKeyWord()
{
	$iptables_command = "/usr/local/bin/iptables -t nat -F KEYWORDS";
	$ret = shell_exec($iptables_command);
	return $ret;
}

function ApplySysKeyWordFilter()
{

	FlushSysRuleForKeyWord();

	$iptables = '/usr/local/bin/iptables -t nat -A KEYWORDS -m string --algo bm --string "';
	$end_string = '" -j DROP;';

	$command = "";
	$kw_list = GetXmlKeyWordList();
	foreach ($kw_list as $l)
	{
		if ($l["Name"] == NULL)
			continue;
		else
			$name = base64_decode($l["Name"]);
		
		$command .= $iptables . $name. $end_string;
	}
	//print $command;
	$ret = shell_exec($command);
	return $ret;
}

?>
